package com.salton123.camera;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.opengl.GLES20;
import android.util.Log;
import android.view.MotionEvent;

import com.tencent.youtu.YTSegmentation;

import java.nio.ByteBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class CameraRenderer extends VirtualBackgroundRenderer {

    private int[] mTextureCameraRGBA = {-1};
    private int[] mTextureSegment = {-1};

    private OESRenderer mYUV2RGBAConverter;
    private DirectRenderer mDirectRenderer;
    private YTSegmentation mSegmentation;
    private Context mContext;

    private ByteBuffer mFrameBuffer;
    private ByteBuffer mMaskBuffer;
    private ByteBuffer mBgBuffer;
    private Bitmap mBgBitmap;
    private int bgWidth;
    private int bgHeight;

    public CameraRenderer(Context context) {
        super();
        mContext = context;
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        super.onSurfaceCreated(gl, config);
        // ----------------------------------------------------
        // 初始化屏幕绘制需要用的renderer
        // ----------------------------------------------------
        mYUV2RGBAConverter = new OESRenderer();
        mYUV2RGBAConverter.init();

        mDirectRenderer = new DirectRenderer();
        mDirectRenderer.init();
        // ----------------------------------------------------
        // 初始化SDK
        // ----------------------------------------------------
        mSegmentation = new YTSegmentation(mContext.getAssets(), "models/v303", "config.ini");
        Log.e("tag", "onCreate: " + YTSegmentation.getVersion());
        // ----------------------------------------------------
        // 初始化喂图所需要的ByteBuffer
        // ----------------------------------------------------
        mFrameBuffer = ByteBuffer.allocateDirect(cameraWidth * cameraHeight * 4);
        mMaskBuffer = ByteBuffer.allocateDirect(cameraWidth * cameraHeight * 4);
        // ----------------------------------------------------
        // 加载背景图
        // ----------------------------------------------------
        loadBG();
        // ----------------------------------------------------
        // 初始化绘制时需要的纹理
        // ----------------------------------------------------
        mTextureCameraRGBA[0] = GlUtil.createTexture(cameraWidth, cameraHeight, GLES20.GL_RGBA);
        mTextureSegment[0] = GlUtil.createTexture(cameraWidth, cameraHeight, GLES20.GL_RGBA);
        // ----------------------------------------------------
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        super.onSurfaceChanged(gl, width, height);
    }


    public void onDrawFrame(GL10 gl) {
        super.onDrawFrame(gl);
        // 将相机数据下载至mFrameBuffer
        // ----------------------------------------------------
        mYUV2RGBAConverter.doRender(mTextureCameraOES, mTextureCameraRGBA[0], cameraWidth, cameraHeight, cameraTransformMatrix, null, mFrameBuffer);
        // ----------------------------------------------------
        // 调用SDK进行人像分割，得到mask
        // ----------------------------------------------------
        mSegmentation.segment(mFrameBuffer, cameraWidth, cameraHeight, mMaskBuffer, null);
        // ----------------------------------------------------
        // 将mask和前景、背景进行合成
        // ----------------------------------------------------
        YTSegmentation.blend(mFrameBuffer, cameraWidth, cameraHeight, mBgBuffer, bgWidth, bgHeight, mMaskBuffer);
        // ----------------------------------------------------
        // 将合成后得到的数据上传至纹理
        // ----------------------------------------------------
        GlUtil.updateTexture(mFrameBuffer, mTextureSegment[0], cameraWidth, cameraHeight, GLES20.GL_RGBA);
        // ----------------------------------------------------
        // 绘制上屏
        // ----------------------------------------------------
        mDirectRenderer.doRender(mTextureSegment[0], -1, windowWidth, windowHeight, null, screenTransformMatrix, null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    public void deinit() {
        super.deinit();

        if (mYUV2RGBAConverter != null) {
            mYUV2RGBAConverter.release();
            mYUV2RGBAConverter = null;
        }
        if (mDirectRenderer != null) {
            mDirectRenderer.release();
            mDirectRenderer = null;
        }

        GLES20.glDeleteTextures(1, mTextureCameraRGBA, 0);
    }

    int backgroundResId = 0;

    private void loadBG() {
        try {
            Drawable d = mContext.getResources().getDrawable(backgroundResId);
            mBgBitmap = FileUtils.drawableToBitmap(d);
            mBgBuffer = ByteBuffer.allocateDirect(mBgBitmap.getWidth() * mBgBitmap.getHeight() * 4);
            mBgBitmap.copyPixelsToBuffer(mBgBuffer);
            bgWidth = mBgBitmap.getWidth();
            bgHeight = mBgBitmap.getHeight();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
